package com.yjhb.android.sbz.activity;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.RefreshPackageAdapter;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.RefreshPackage;
import com.yjhb.android.sbz.mvp.refreshpackage.RefreshPackagePresenter;
import com.yjhb.android.sbz.mvp.refreshpackage.RefreshPackageView;
import com.yjhb.android.sbz.tool.LoadingDialogTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.dialog.ILoading;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/7 9:39
 * <p>
 * 刷新包
 */
public class RefreshPackageActivity extends BaseActivity<RefreshPackagePresenter> implements BaseRecyclerAdapter.ItemClickListener<RefreshPackage>, RefreshPackageView {
    private final String TAG = "RefreshPackageActivity";
    @BindView(R.id.num_tv)
    TextView num_tv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.depict_tv)
    TextView depict_tv;

    RefreshPackageAdapter refreshPackageAdapter;
    List<RefreshPackage> refreshPackages = new ArrayList<>();

    ILoading loadingDialog;

    @Override
    protected RefreshPackagePresenter initPresenter() {
        return new RefreshPackagePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_refresh_package;
    }

    @Override
    protected void initUI() {
        CharSequence charSequence = "刷新包购买后不支持退订，请确定后购买。会员每日刷新首次免费，不消耗次数。";
//        String highLight = "不支持退订";
//        int index = charSequence.toString().indexOf(highLight);
//        depict_tv.setText(ZxSpanTool.ColorSpan(charSequence, index, index + highLight.length(), Color.RED));
        depict_tv.setText(charSequence);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(refreshPackageAdapter = new RefreshPackageAdapter(this));
        initRefreshPackages();
        refreshPackageAdapter.setData(refreshPackages);
        loadingDialog = LoadingDialogTool.createDialog(this);
    }

    private void initRefreshPackages() {
        refreshPackages.clear();
        refreshPackages.add(new RefreshPackage(10, new BigDecimal("5")));
        refreshPackages.add(new RefreshPackage(25, new BigDecimal("10")));
        refreshPackages.add(new RefreshPackage(68, new BigDecimal("25")));
        refreshPackages.add(new RefreshPackage(148, new BigDecimal("50")));
        refreshPackages.add(new RefreshPackage(318, new BigDecimal("100")));
        refreshPackages.add(new RefreshPackage(688, new BigDecimal("200")));
        refreshPackages.add(new RefreshPackage(1088, new BigDecimal("300")));
        refreshPackages.add(new RefreshPackage(1888, new BigDecimal("500")));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {
        refreshPackageAdapter.setOnItemClick(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @OnClick(R.id.buy_tv)
    public void buy_tv() {
        presenter.buyFlush(refreshPackages.get(refreshPackageAdapter.getSelectedIndex()).getMoney());
    }

    @OnClick(R.id.open_vip_tv)
    public void open_vip_tv() {
        ZxActivityTool.skipActivity(this, VipCenterActivity.class);
    }

    @OnClick(R.id.use_record)
    public void use_record() {
        ZxActivityTool.skipActivity(this, RefreshRecordActivity.class);
    }

    @Override
    public void onItemClick(int index, RefreshPackage refreshPackage) {
        refreshPackageAdapter.setSelectedIndex(index);
        refreshPackageAdapter.notifyDataSetChanged();
    }

    @Override
    public void onViewClick(View view, String msg, int index, RefreshPackage refreshPackage) {

    }

    @Override
    public void refresh() {
        num_tv.setText(App.userInfo.getPayFlushCount() + "次");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(EventMsg msg) {
        if (msg.msg == EventMsg.USER_UPDATE_OK) {
            refresh();
        }
    }

    @Override
    public void onWait() {
        loadingDialog.show();
    }

    @Override
    public void onBuy() {
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE, null));
        loadingDialog.cancel();
        ZxToast.showSuccessCenterToast("购买成功！");
    }


    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        //余额不足
        if (code == 1110) {
            ZxToast.showINFOBottomToast("余额不足，请充值！");
            Tool.startRechargeActivity(this, 2, null, true);
            return;
        }
        ZxToast.showFAILCenterToast("失败：" + msg);
    }
}
